<?php
/*
 * classtest.php Created on 2013-4-24
 * Version:0.4
 * last modified 2013-06-04 by 46673405@qq.com
 *
 * 待改进：
 * cheackImgSize只检查后缀 应考虑是否换用检查文件mime来检查
 * 缩放比例只完成拉伸
 * 考虑是否统一宽高顺序
 * 换用更精确的方法测量文字水印的字符串宽度
 *
 *  考虑是否将返回统一为 array (true,message);
 * [0]=>true/false
 * [1]=>res/errorNum like '202'
 * [2]=NULL/errorFunction like 'checkImgType()'
 * [3]=NULL/errorMessage like 'wrong Img Type';
 *
 *
 *	用例部分
 *  1、在HTML页面中构建一个表单 enctype 类型为 "multipart/form-data" 上传程序将通过form的name属性来获得上传的文件信息 action指向调用上传类的文件
 *
 *  <form method="post" action="#" name="form" enctype="multipart/form-data">
 * 	<input type="file" name="upload" />
 *  <input type="submit" />
 * 	</form>
 *
 *  2、调用上传类的文件中调用代码
 *
 *  require ("icheesUpImg.class.php");
 *  $img = new icheesUpImg;
 *
 *
 *  文件上传：uploadFile()
 *	$res = $img -> uploadFile('upload');
 *	返回样例 upfiles/files/20130425_041244_22405.rar
 *	参数为form name
 *
 *
 *	图像上传：uploadImg()
 *	$res = $img -> uploadImg('upload');
 *	返回样例 upfiles/images/20130425_043407_29554.jpg
 *	参数为form name
 *
 *
 *	图像缩放：resizeImg()
 *	$res = $img -> resizeImg("upfiles/images/20130425_045103_21361.jpg",300,300,'01','_resize');
 *	参数依次为： 图片地址,目标尺寸,缩放方法,缩放后生成图像后缀
 *	返回样例 upfiles/resize/20130425_045103_21361_resize.jpg
 *	ps:如获得图像后缀，则生成带有图像后缀的新图像。如未获得，则直接将原图像加水印
 *
 *
 *	图像缩放固定方法-生成主图：resizeBigImg()
 *	$res = $img -> resizeBigImg("upfiles/images/20130425_045103_21361.jpg");
 *	参数为原图像地址
 *	返回样例 upfiles/large/20130425_045103_21361_large.jpg
 *
 *
 *	图像缩放固定方法-生成缩略图：resizeSmallImg()
 *	$res = $img -> resizeSmallImg("upfiles/images/20130425_045103_21361.jpg");
 *	参数为原图像地址
 *	返回样例 upfiles/small/20130425_045103_21361_small.jpg
 *
 *
 *	为图像添加文字水印：waterMarkTxt()
 *	$res = $img -> waterMarkTxt("upfiles/large/20130425_045103_21361_large.jpg",'中文水印测试|waterMarkTest|透かしテスト',0,0,0,'14','ttf/msyh.ttf','100','200','0','60','_mark');
 *	参数依次为：原图像地址,水印字符串(中日韩),水印字体地址,水平位置，垂直位置，角度，质量，后缀
 *	返回样例：20130425_045103_21361_large_mark.jpg
 *	ps:如获得图像后缀，则生成带有图像后缀的新图像。如未获得，则直接将原图像加水印
 *
 *
 *	图像文字水印固定方法:waterMarkTxtFix()
 *	$res = $img -> waterMarkTxtFix("upfiles/large/20130425_045103_21361_large.jpg");
 *	参数为图像地址
 *	返回样例:upfiles/large/20130425_045103_21361_large.jpg
 *
 *
 *	图像图片水印：waterMarkPic()
 *	$res= $img ->waterMarkPic("upfiles/large/20130425_045103_21361_large.jpg","upfiles/small/20130425_045103_21361_small.jpg",200,200,80,60,'_mark');
 *	参数依次为：图像地址,水印图片地址,水平位置,垂直位置,透明度,质量,后缀
 *	返回样例：20130425_045103_21361_large_mark.jpg
 *	ps:如获得图像后缀，则生成带有图像后缀的新图像。如未获得，则直接将原图像加水印
 *
 *
 *	图像图片水印：waterMarkPicFix()
 *	$res= $img ->waterMarkPicFix("upfiles/large/20130425_045103_21361_large.jpg");
 *	参数为图像地址
 *	返回样例：20130425_045103_21361_large_mark.jpg
 *
 */
class icheesFileUpload {

	//////////////////// ICE 通用配置部分 upCommon_ ///////////////////

	var $upCommon_maxSize = 2048 ; //ICE 上传文件最大尺寸 单位为KB ICE//

	var $upCommon_folder = "upfiles/";  //ICE 上传根目录 ICE//

	//////////////////// ICE 文件上传配置 upFile_ ///////////////////

	var $upFile_type = array('pdf','doc','docx','xls','xlsx','ppt','pptx','txt','rar','zip','7z'); //允许的文件格式

	var $upFile_folder = "files/" ; //ICE 上传文件夹 ICE//

	//////////////////// ICE 图片上传配置 upImg_ ///////////////////

	var $upImg_type = array('jpg','png','gif'); //允许的图片格式

	var $upImg_folder = "images/" ; //ICE 上传图片文件夹 ICE//

	var $upImg_maxWidth = 10000 ; //ICE 图片最大宽度 ICE//

	var $upImg_maxHeight = 10000 ; //ICE 图片最大高度 ICE//

	var $upImg_mixWidth = 200 ; //ICE 图片最小宽度 ICE//

	var $upImg_mixHeight = 200 ; //ICE 图片最小高度 ICE//

	//////////////////// ICE 缩放配置 imgResize_ ///////////////////

	var $imgResize_method = 01; //ICE 01:直接缩放 ICE//

	var $imgResize_backgroundColor = array (255,255,255); //ICE 255,255,255白色 0,0,0黑色 ICE//

	var $imgResize_folder = "resize/" ; //ICE 缩放后图片存放文件夹 ICE//

	var $imgResize_bigImgFolder = "large/"; //ICE 产品主图存放文件夹 ICE //

	var $imgResize_bigImgWidth = 800; //ICE 产品主图宽度 ICE //

	var $imgResize_bigImgHeight = 800; //ICE 产品主图高度 ICE //

	var $imgResize_smallImgFolder ="small/"; //ICE 缩略图存放文件夹 ICE//

	var $imgResize_smallImgWidth = 200; //ICE 缩略图宽度 ICE //

	var $imgResize_smallImgHeight = 200; //ICE 缩略图高度 ICE //

	//////////////////// ICE 文字水印配置 waterMarkTxt_  ///////////////////

    var $waterMarkTxt_string = '吉士 ichees.com';

    var $waterMarkTxt_fontColor = array('0','0','0');

    var $waterMarkTxt_fontSize = 9;

    var $waterMarkTxt_fontTypeSrc = 'ttf/msyh.ttf';

    var $waterMarkTxt_postion = 5; //0随机 1左上 2中上 3右上 4左中 5中 6右中 7左下 8中下 9右下 默认5居中

    var $waterMarkTxt_angle = 0;

	var $waterMarkTxt_quality = 60;

	var $waterMarkTxt_suffix = '_mark';

	//////////////////// ICE 图片水印配置 waterMarkPic_ ///////////////////

    var $waterMarkPic_markSrc = 'waterMarkImg/waterMark.png';

    var $waterMarkPic_postion = 0; //0随机 1左上 2中上 3右上 4左中 5中 6右中 7左下 8中下 9右下 默认5居中

	var $waterMarkPic_aphla = 60;

	var $waterMarkPic_quality = 60;

	var $waterMarkPic_suffix = '_mark';

	//////////////////// ICE 其他配置  ////////////////////


    function __construct() {
    	$check = $this->checkFolders($this->upCommon_folder); //检查上传根目录是否存在 如不存在则建立
		if ($check != 'success') {
			return array(
				'result' => false,
				'resCode' => '101',
				'resMessage' => 'error:can not great upfile root floder');
		}
    }

 	function upLoadFile($inputName) {
 	/*
 	 * 文件上传
 	 * $inputName必须 否则返回""
 	 * 检查文件后缀与文件尺寸
 	 * 如果检查通过则将上传文件从临时文件夹移动至目标文件夹
 	 * 返回生成的随机文件名
 	 *
 	 */

 	 	//检查是否有上传的文件
		if(empty($_FILES[$inputName]["name"])) {
			return array(
				'result' => false,
				'resCode' => '201',
				'resMessage' => 'error:lose input info');
		}

		//检查文件类型
		$check = $this->checkType($inputName,$this->upFile_type);
		if ($check != 'success') {return $check;}

		//检查文件类型
		$check = $this->checkSize($inputName,$this->upCommon_maxSize);
		if ($check != 'success') {return $check;}

		//检查上传目标文件夹是否存在 不存在则建立
		$upTargetFloder= $this->upCommon_folder.$this->upFile_folder;
		$check = $this->checkFolders($upTargetFloder);
		if ($check != 'success') {return $check;}

		//生成随机文件名
		$fileName = explode(".",$_FILES[$inputName]["name"]) ;
		$fileCount = count($fileName) ;
		$fileType = strtolower($fileName[$fileCount-1]) ;
		$randName = $this->randName('',$fileType);

		//将临时目录中的上传文件移动至目标目录
		$upTargetSrc = $upTargetFloder.$randName;
		$upResult = move_uploaded_file($_FILES[$inputName]["tmp_name"],$upTargetSrc);
		if (!$upResult)	{
			return array( 
			'result' => false,
			'resCode' => '202',
			'resMessage' => 'Move tempFloder to targetFloder error');
		}

		//操作完成 返回文件保存路径
		return $upTargetSrc;
	}

	function uploadImg ($inputName) {
 	/*
 	 * 图片上传方法
 	 * 检查图片后缀与图片大小及图片尺寸
 	 * 如果检查通过则将上传文件从临时文件夹移动至目标文件夹
 	 * 返回生成的随机文件名
 	 *
 	 */

 	 	//检查是否有上传的图片
		if(empty($_FILES[$inputName]["name"])) {
			return array (
				'result' => false,
				'resCode' => '203',
				'resMessage' => 'error:Lose the input name'
				);
		}

		//检查图片类型
		$check = $this->checkType($inputName,$this->upImg_type);
		if ($check != 'success') {return $check;}

		//检查图片大小
		$check = $this->checkSize($inputName,$this->upCommon_maxSize);
		if ($check != 'success') {return $check;}

		//检查图片尺寸
		$check = $this->checkImgSize ($inputName,$this->upImg_maxHeight,$this->upImg_maxWidth,$this->upImg_mixHeight,$this->upImg_mixWidth);
		if ($check != 'success') {return $check;}

		//检查上传目标文件夹是否存在 不存在则建立
		$upTargetFloder= $this->upCommon_folder.$this->upImg_folder;
		$check = $this->checkFolders($upTargetFloder);
		if ($check != 'success') {return $check;}

		//生成随机文件名
		$fileName = explode(".",$_FILES[$inputName]["name"]) ;
		$fileCount = count($fileName) ;
		$fileType = strtolower($fileName[$fileCount-1]) ;
		$randName = $this->randName('',$fileType);

		//将临时目录中的上传文件移动至目标目录
		$upTargetSrc = $upTargetFloder.$randName;
		$upResult = move_uploaded_file($_FILES[$inputName]["tmp_name"],$upTargetSrc);
		if (!$upResult)	{
			return array( 
			'result' => false,
			'resCode' => '204',
			'resMessage' => 'error:Move tempFloder to targetFloder error');
		}

		//操作完成 返回文件保存路径
		return $upTargetSrc;
	}



	function resizeImg ($originalSrc,$targetWidth,$targetHeight,$method,$suffix) {
 	/*
 	 * 图片缩放
 	 * 需获得原始图片地址,目标宽高,新生成图片的后缀 如果未获得上述变量则返回 'error:resizeImg() Lose the necessary parameters'
 	 * 返回缩放后尺寸的存放地址
 	 * 未设置缩放比例
 	 */
		if (empty($originalSrc) or empty($targetHeight) or empty($targetWidth) or empty($suffix)) {
			return array(
				'result' => false,
				'resCode' => '205',
				'resMessage' => 'error: resizeImg() Lose the necessary parameters');
		}

		//获得原图片文件名
		$originalImgFullName = ltrim(strrchr($originalSrc,'/'),'/');
		$imgType = ltrim(strrchr($originalSrc,'.'),'.');

		//生成新图片名
		$targetImgName = rtrim($originalImgFullName,'.'.$imgType);
		$targetImgFullName = $targetImgName.$suffix.'.'.$imgType;

		//获得原图片尺寸
		$originalInfo = getimagesize($originalSrc);

		//根据缩放方法获得原图缩放尺寸范围
		$newSize = $this->getNewSize($originalInfo['0'],$originalInfo['1'],$targetWidth,$targetHeight,$method);
		$originalWidth = $newSize[0];
		$originalHeight = $newSize[1];


		//检查上传目标文件夹是否存在 不存在则建立
		$upTargetFloder= $this->upCommon_folder.$this->imgResize_folder;
		$check = $this->checkFolders($upTargetFloder);
		if ($check != 'success') {return $check;}

		//建立新图像
		//分图片类型进行新建 图片类型由getimagesize中的mime获得
		switch ($originalInfo['mime']) {
			case 'image/jpeg':
				$originalImg = imagecreatefromjpeg($originalSrc);
				break;
			case 'image/png':
				$originalImg = imagecreatefrompng($originalSrc);
				break;
			case 'image/gif':
				$originalImg = imagecreatefromgif($originalSrc);
				break;
			default:
				return array (
					'result' => false,
					'resCode' => '206',
					'resMessage' => 'error:resizeImg() not support resize from this fileType');
				break;
			}
		if (function_exists("imagecreatetruecolor")) {
			$targetImg = imagecreatetruecolor($targetWidth, $targetHeight) ;

			//填充背景颜色
			$color = imagecolorAllocate($targetImg,$this->imgResize_backgroundColor[0],$this->imgResize_backgroundColor[1],$this->imgResize_backgroundColor[2]);
			imagefill($targetImg,0,0,$color);

			imagecopyresampled($targetImg, $originalImg, 0, 0, 0, 0, $targetWidth, $targetHeight, $originalWidth, $originalHeight) ;
		}
		else {
			$targetImg = imagecreate($targetWidth, $targetHeight);
			// 填充背景颜色
			imagecolorallocate($targetImg,$this->imgResize_backgroundColor[0],$this->imgResize_backgroundColor[1],$this->imgResize_backgroundColor[2]);

			imagecopyresampled($targetImg, $originalImg, 0, 0, 0, 0, $targetWidth, $targetHeight, $originalWidth, $originalHeight) ;
		}

		//输出新图像
		if (file_exists($this->upCommon_folder."/".$this->imgResize_folder."/".$targetImgFullName)) { //如果存在同名文件则删除
			@unlink($this->annexFolder."/".$this->smallFolder."/".$targetImgFullName) ;
		}
		//输出新生成的图片
			switch ($originalInfo['mime']) {
				case 'image/jpeg':
					imagejpeg($targetImg,$this->upCommon_folder."/".$this->imgResize_folder."/".$targetImgFullName) ;
					break;
				case 'image/png':
					imagepng($targetImg,$this->upCommon_folder."/".$this->imgResize_folder."/".$targetImgFullName) ;
					break;
				case 'image/gif':
					imagegif($targetImg,$this->upCommon_folder."/".$this->imgResize_folder."/".$targetImgFullName) ;
					break;
				default:
					return array (
					'result' => false,
					'resCode' => '207',
					'resMessage' => 'error:resizeImg() not support resize from this fileType');
					break;
			}

		//销毁内存中的图像
		imagedestroy($targetImg);
		imagedestroy($originalImg);

		//返回图片路径
		return $this->upCommon_folder.$this->imgResize_folder.$targetImgFullName ; //返回新图片路径

	}

	function resizeBigImg ($originalSrc) {
 	/*
 	 * ressizeImg的固定尺寸方法
 	 * 需获得原始图片地址
 	 * 返回缩放后尺寸的存放地址
	 */
	 $this->imgResize_folder = $this->imgResize_bigImgFolder;
	 $bigImgSrc = $this->resizeImg ($originalSrc,$this->imgResize_bigImgWidth,$this->imgResize_bigImgHeight,$this->imgResize_method,'_large');
 	 return $bigImgSrc;
	}

	function resizeSmallImg ($originalSrc) {
 	/*
 	 * ressizeImg的固定尺寸方法
 	 * 需获得原始图片地址
 	 * 返回缩放后尺寸的存放地址
	 */
	 $this->imgResize_folder = $this->imgResize_smallImgFolder;
	 $SmallImgSrc = $this->resizeImg ($originalSrc,$this->imgResize_smallImgWidth,$this->imgResize_smallImgHeight,$this->imgResize_method,'_small');
 	 return $SmallImgSrc;
	}

	function waterMarkTxt ($originalSrc,$text,$fontColorR,$fontColorG,$fontColorB,$fontSize,$fontTypeSrc,$marginLeft,$marginTop,$angle,$quality,$suffix) {
 	/*
 	 * 文字水印
 	 * 图片地址,水印文字,颜色R,G,B,文字字号,字体文件路径,水平偏移,垂直偏移,角度,jpeg时压缩质量，[后缀]可选 如为空 则直接替换原图
 	 * 返回水印后图片地址
 	 *
 	 */

		//检查获得的原文件路径下是否存在对应的图片文件
		if(!file_exists($originalSrc)) {return 'error:watermarkTxt() originalImage not exists';}

		//获得原图片类型与尺寸
		$originalInfo = getimagesize($originalSrc);
		$originalWidth = $originalInfo['0'];
		$originalHeight = $originalInfo['1'];

        //建立新画布
		switch ($originalInfo['mime']) {
			case 'image/jpeg':
				$originalImg = imagecreatefromjpeg($originalSrc);
				break;
			case 'image/png':
				$originalImg = imagecreatefrompng($originalSrc);
				break;
			case 'image/gif':
				$originalImg = imagecreatefromgif($originalSrc);
				break;
			default:
				return array (
					'result' => false,
					'resCode' => '208',
					'resMessage' => 'error:waterMark() not support make waterMark from this fileType');
				break;
		}
		//将原图置入新画布
		if (function_exists("imagecreatetruecolor")) {
			$targetImg = imagecreatetruecolor($originalWidth, $originalHeight) ;

			//填充背景颜色
			$color = imagecolorAllocate($targetImg,0,0,0);
			imagefill($targetImg,0,0,$color);
			imagecopyresampled($targetImg, $originalImg, 0, 0, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight) ;
		}
		else {
			$targetImg = imagecreate($originalWidth, $originalHeight);
			// 填充背景颜色
			imagecolorallocate($targetImg,0,0,0);
			imagecopyresampled($targetImg, $originalImg, 0, 0, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight) ;
		}

		//写入水印文字


		//$originalSrc,$text,$fontColorR,$fontColorG,$fontColorB,$waterMarkAlpha,$fontSize,$fontTypeSrc,$marginLeft,$marginTop,$angle

		//水平与垂直位置

		//确定颜色并写入文字
		$fontColor = ImageColorAllocate($targetImg, $fontColorR,$fontColorG,$fontColorB);
		$res = imagettftext($targetImg,$fontSize,$angle,$marginLeft,$marginTop,$fontColor,$fontTypeSrc,$text);
		if(!$res) {
			return array (
			'result' => false,
			'resCode' => '209',
			'resMessage' => 'error:waterMark() imagettftext false');
		}




		if ($suffix) { //如果获得后缀，则生成加入后缀的新文件名 否则删除旧图 按原文件名直接输出新图
			//获得原图片名 并加后缀
			$originalImgFullName = ltrim(strrchr($originalSrc,'/'),'/');
			$dotfileType = strrchr($originalSrc,'.');
			$originalImgName = rtrim($originalImgFullName,$dotfileType);
			$targetSrc =$originalImgName.$suffix.$dotfileType;
			}
		else {//删除原图
		$targetSrc = $originalSrc;
		unlink($originalSrc) ;
		}

		//输出新生成的图片
			switch ($originalInfo['mime']) {
				case 'image/jpeg':
					imagejpeg($targetImg,$targetSrc,$quality) ;
					break;
				case 'image/png':
					imagepng($targetImg,$targetSrc) ;
					break;
				case 'image/gif':
					imagegif($targetImg,$targetSrc) ;
					break;
				default:
					return array (
					'result' => false,
					'resCode' => '210',
					'resMessage' => 'error:resizeImg() not support resize from this fileType');
					break;
			}

		//销毁内存中的图像
		imagedestroy($targetImg);
		imagedestroy($originalImg);

		//返回图片路径
		return $targetSrc ; //返回新图片路径
	}

	function waterMarkTxtFix ($originalSrc) {
 	/*
 	 * 固定的文字水印方法
 	 * 图片地址
 	 * 返回水印后图片地址
 	 *
 	 */


		//字数乘字号获得字符串宽度(px)
		$stringWidth = mb_strwidth($this->waterMarkTxt_string,'utf8')*$this->waterMarkTxt_fontSize*0.68;  //字体的宽高比是个问题 获得的字符串宽度不准
		//更为准确的获得文字宽度的方法
		/*
		 *  $f = 'C:/WINDOWS/Fonts/ARIAL.TTF';
			$s = 'IIIIIIIIWWWWllllLoOWi中华人民共和国';
			$z = 14;
			$b = imagettfbbox($z,0,$f,$s);
			$w = abs($b[2] - $b[0]);
			$h = abs($b[5] - $b[3]);

		 */
		$stringHeight = $this->waterMarkTxt_fontSize;
		//获得原图片类型与尺寸
		$originalInfo = getimagesize($originalSrc);
		$originalWidth = $originalInfo['0'];
		$originalHeight = $originalInfo['1'];
		//根据水印方法编号 返回水印
		switch($this->waterMarkTxt_postion){
			case 0: //随机位置
				$marginLeft=rand(0,($originalWidth-$stringWidth));
				$marginTop=rand(0,($originalWidth-$stringHeight));
				break;
			case 1: //左上 平分三份取中央减去水印字符串宽度一半
				$marginLeft=$originalWidth/3/2-$stringWidth/2;
				$marginTop=$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=0:$marginLeft;
				break;
			case 2: //中上 平分三份取两份取中央减去水印字符串宽度一半
				$marginLeft=($originalWidth/3)*2-$originalWidth/3/2-$stringWidth/2;
				$marginTop=$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=$originalWidth/2-$stringWidth/2:$marginLeft;
				break;
			case 3: //右上
				$marginLeft=($originalWidth/3)*3-$originalWidth/3/2-$stringWidth/2;
				$marginTop=$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=$originalWidth-$stringWidth:$marginLeft;
				break;
			case 4: //左中
				$marginLeft=$originalWidth/3/2-$stringWidth/2;
				$marginTop=($originalHeight/3)*2-$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=0:$marginLeft;
				break;
			case 5: //居中
				$marginLeft=($originalWidth/3)*2-$originalWidth/3/2-$stringWidth/2;
				$marginTop=($originalHeight/3)*2-$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=$originalWidth/2-$stringWidth/2:$marginLeft;
				break;
			case 6: //右中
				$marginLeft=($originalWidth/3)*3-$originalWidth/3/2-$stringWidth/2;
				$marginTop=($originalHeight/3)*2-$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=$originalWidth-$stringWidth:$marginLeft;
				break;
			case 7: //下左
				$marginLeft=$originalWidth/3/2-$stringWidth/2;
				$marginTop=($originalHeight/3)*3-$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=0:$marginLeft;
				break;
			case 8: //下中
				$marginLeft=($originalWidth/3)*2-$originalWidth/3/2-$stringWidth/2;
				$marginTop=($originalHeight/3)*3-$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=$originalWidth/2-$stringWidth/2:$marginLeft;
				break;
			case 9: //下右
				$marginLeft=($originalWidth/3)*3-$originalWidth/3/2-$stringWidth/2;
				$marginTop=($originalHeight/3)*3-$originalHeight/3/2-$stringHeight/2;
				$originalWidth/3-$stringWidth<0?$marginLeft=$originalWidth-$stringWidth:$marginLeft;
				break;
			default: break;
		}

 		$res = $this->waterMarkTxt($originalSrc,$this->waterMarkTxt_string,$this->waterMarkTxt_fontColor[0],$this->waterMarkTxt_fontColor[1],$this->waterMarkTxt_fontColor[2],$this->waterMarkTxt_fontSize,$this->waterMarkTxt_fontTypeSrc,$marginLeft,$marginTop,$this->waterMarkTxt_angle,$this->waterMarkTxt_quality,$this->waterMarkTxt_suffix);
		return $res;
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function waterMarkPic ($originalSrc,$waterMarkSrc,$marginLeft,$marginTop,$aphla,$quality,$suffix) {
 	/*
 	 * 图片水印
 	 * 图片地址 水印图片地址
 	 * 返回水印后图片地址
 	 *
 	 */

		//检查获得的原文件路径下是否存在对应的图片文件
		if(!file_exists($originalSrc)) {
			return array (
			'result' => false,
			'resCode' => '211',
			'resMessage' => 'error:waterMarkPic() originalImage not exists');
		}
		if(!file_exists($waterMarkSrc)) {return 'error:waterMarkPic() waterMarkImage not exists';}

		//获得原图片类型与尺寸
		$originalInfo = getimagesize($originalSrc);
		$originalWidth = $originalInfo['0'];
		$originalHeight = $originalInfo['1'];

        //建立新画布
		switch ($originalInfo['mime']) {
			case 'image/jpeg':
				$originalImg = imagecreatefromjpeg($originalSrc);
				break;
			case 'image/png':
				$originalImg = imagecreatefrompng($originalSrc);
				break;
			case 'image/gif':
				$originalImg = imagecreatefromgif($originalSrc);
				break;
			default:
				return array (
					'result' => false,
					'resCode' => '212',
					'resMessage' => 'error:waterMarkPic() not support make waterMark from this fileType');
				break;
		}

		//将原图置入新画布
		if (function_exists("imagecreatetruecolor")) {
			$targetImg = imagecreatetruecolor($originalWidth, $originalHeight) ;
			//填充背景颜色
			$color = imagecolorAllocate($targetImg,0,0,0);
			imagefill($targetImg,0,0,$color);
			imagecopyresampled($targetImg, $originalImg, 0, 0, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight) ;
		}
		else {
			$targetImg = imagecreate($originalWidth, $originalHeight);
			// 填充背景颜色
			imagecolorallocate($targetImg,0,0,0);
			imagecopyresampled($targetImg, $originalImg, 0, 0, 0, 0, $originalWidth, $originalHeight, $originalWidth, $originalHeight) ;
		}


/////


		//获得水印图片类型与尺寸
		$waterMarkInfo = getimagesize($waterMarkSrc);
		$waterMarkWidth = $waterMarkInfo['0'];
		$waterMarkHeight = $waterMarkInfo['1'];

		//调入水印图片
		switch ($waterMarkInfo['mime']) {
			case 'image/jpeg':
				$waterMarkImg = imagecreatefromjpeg($waterMarkSrc);
				break;
			case 'image/png':
				$originalImg = imagecreatefrompng($originalSrc);
				break;
			case 'image/gif':
				$originalImg = imagecreatefromgif($originalSrc);
				break;
			default:
				return array (
					'result' => false,
					'resCode' => '213',
					'resMessage' => 'error:waterMarkPic() not support this waterMark fileType');
				break;
		}
/////
		//置入水印图片

		//$originalSrc,$waterMarkSrc,$marginLeft,$marginTop,$angle,$quality,$suffix
		//建立混色模式
		$res = imagealphablending($targetImg, true);
		if(!$res) {
			return array (
			'result' => false,
			'resCode' => '214',
			'resMessage' => 'error:waterMarkPic() imagealphablending false');
		}
		//在填充原图片的画布中 置入水印图片
		$res = imagecopymerge($targetImg,$waterMarkImg,$marginLeft,$marginTop,0,0,$waterMarkWidth,$waterMarkHeight,$aphla);

		if ($suffix) { //如果获得后缀，则生成加入后缀的新文件名 否则删除旧图 按原文件名直接输出新图
			//获得原图片名 并加后缀
			$originalImgFullName = ltrim(strrchr($originalSrc,'/'),'/');
			$dotfileType = strrchr($originalSrc,'.');
			$originalImgName = rtrim($originalImgFullName,$dotfileType);
			$targetSrc =$originalImgName.$suffix.$dotfileType;
			}
		else {//删除原图
		$targetSrc = $originalSrc;
		unlink($originalSrc) ;
		}

		//输出新生成的图片
			switch ($originalInfo['mime']) {
				case 'image/jpeg':
					imagejpeg($targetImg,$targetSrc,$quality) ;
					break;
				case 'image/png':
					imagepng($targetImg,$targetSrc) ;
					break;
				case 'image/gif':
					imagegif($targetImg,$targetSrc) ;
					break;
				default:
					return array (
					'result' => false,
					'resCode' => '215',
					'resMessage' => 'error:waterMarkPic() not support make waterMark from this fileType');
					break;
			}

		//销毁内存中的图像
		imagedestroy($targetImg);
		imagedestroy($originalImg);
		imagedestroy($waterMarkImg);

		//返回图片路径
		return $targetSrc ; //返回新图片路径
	}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

	function waterMarkPicFix ($originalSrc) {
 	/*
 	 * 图片水印的固定方法
 	 * 图片地址,水印图片地址,水印图片透明度,水平位置百分比,垂直位置百分比,水印图片后缀
 	 * 返回水印后图片地址
 	 *
 	 */

 	 /*
	    var $waterMarkPic_markSrc = 'upfiles/small/20130425_045103_21361_small.jpg';
	    var $waterMarkPic_postion = 0; //0随机 1左上 2中上 3右上 4左中 5中 6右中 7左下 8中下 9右下 默认5居中
		var $waterMarkPic_aphla = 60;
		var $waterMarkPic_quality = 60;
		var $waterMarkPic_suffix = '_mark';
 	  */

		//获得水印图片的尺寸
		$waterMarkInfo = getimagesize($this->waterMarkPic_markSrc);
		$waterMarkWidth = $waterMarkInfo['0'];
		$waterMarkHeight = $waterMarkInfo['1'];

		//获得原图片类型与尺寸
		$originalInfo = getimagesize($originalSrc);
		$originalWidth = $originalInfo['0'];
		$originalHeight = $originalInfo['1'];
		//根据水印方法编号 返回水印
		switch($this->waterMarkPic_postion){
			case 0: //随机位置
				$marginLeft=rand(0,($originalWidth-$waterMarkWidth));
				$marginTop=rand(0,($originalWidth-$waterMarkHeight));
				break;
			case 1: //左上 平分三份取中央减去水印字符串宽度一半
				$marginLeft=$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=0:$marginLeft;//水印图片超出原图三分之一的处理方法
				$originalHeight/3-$waterMarkHeight<0?$marginTop=0:$marginTop;
				break;
			case 2: //中上 平分三份取两份取中央减去水印字符串宽度一半
				$marginLeft=($originalWidth/3)*2-$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=$originalWidth/2-$waterMarkWidth/2:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=0:$marginTop;
				break;
			case 3: //右上
				$marginLeft=($originalWidth/3)*3-$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=$originalWidth-$waterMarkWidth:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=0:$marginTop;
				break;
			case 4: //左中
				$marginLeft=$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=($originalHeight/3)*2-$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=0:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=$originalHeight/2-$waterMarkHeight/2:$marginTop;
				break;
			case 5: //居中
				$marginLeft=($originalWidth/3)*2-$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=($originalHeight/3)*2-$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=$originalWidth/2-$waterMarkWidth/2:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=$originalHeight/2-$waterMarkHeight/2:$marginTop;
				break;
			case 6: //右中
				$marginLeft=($originalWidth/3)*3-$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=($originalHeight/3)*2-$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=$originalWidth-$waterMarkWidth:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=$originalHeight/2-$waterMarkHeight/2:$marginTop;
				break;
			case 7: //下左
				$marginLeft=$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=($originalHeight/3)*3-$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=$originalWidth-$waterMarkWidth:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=0:$marginTop;
				break;
			case 8: //下中
				$marginLeft=($originalWidth/3)*2-$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=($originalHeight/3)*3-$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=$originalWidth/2-$waterMarkWidth/2:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=0:$marginTop;
				break;
			case 9: //下右
				$marginLeft=($originalWidth/3)*3-$originalWidth/3/2-$waterMarkWidth/2;
				$marginTop=($originalHeight/3)*3-$originalHeight/3/2-$waterMarkHeight/2;
				$originalWidth/3-$waterMarkWidth<0?$marginLeft=$originalWidth-$waterMarkWidth:$marginLeft;
				$originalHeight/3-$waterMarkHeight<0?$marginTop=0:$marginTop;
				break;
			default: break;
		}
 		$res = $this->waterMarkPic($originalSrc,$this->waterMarkPic_markSrc,$marginLeft,$marginTop,$this->waterMarkPic_aphla,$this->waterMarkPic_quality,$this->waterMarkPic_suffix);
		return $res;

	}

	function randName($suffix,$imgType){
	/*
	 * 生成一个随机文件名
	 * 文件名格式为 当前时间_随机数_后缀.文件类型
	 * 后缀可选,文件类型不能为空 否则返回 Lose the necessary parameters
	 * 成功后返回文件名
	 *
	 */
	 	if (empty($imgType)) {return 'error:Lose the necessary parameters';}
		$imageName = date("Ymd_His")."_".mt_rand(20000,30000);
		$imageName = $imageName.$suffix.'.'.$imgType ;
		return $imageName;
	}

	function checkType ($inputName,$allowTypeList) {
	/*
	 * 检查上传的临时文件后缀是否在允许上传的后缀列表中
	 * 两个参数都是必要的 否则返回 Lose the necessary parameters
	 * 如果成功 返回true
	 * 如果不成功返回 Not allow file types
	 *
	 */
		if (empty($inputName) or empty($allowTypeList)) {return 'error:Lose the necessary parameters';}

			$fileName = explode(".",$_FILES[$inputName]["name"]) ;
			$fileCount = count($fileName) ;
			$fileType = strtolower($fileName[$fileCount-1]) ;
			$res = in_array($fileType,$allowTypeList);
			if ($res) {
				return 'success';
			}
			else {
				return array (
				'result' => false,
				'resCode' => '301',
				'resMessage' => 'error:Not allow file types');
			}
		}



	function checkFolders($dir){
	/*
	 * 检查目录是否存在
	 * 如果存在 返回 true
	 * $dir 是必要参数 否则返回 Lose the necessary parameters
	 * 如果不存在 尝试建立此文件夹并返回 true
	 * 如果建立文件夹失败 则返回 Can not great floder
	 *
	 */
	 	if (empty($dir)) {
	 		return array (
				'result' => false,
				'resCode' => '302',
				'resMessage' => 'error:checkFolders() Lose the necessary parameters';)
	 	}
		if(is_dir($dir)) {
			return 'success';
		}
		else {
			$res = mkdir($dir, 0777);
			if ($res)
			{return 'success';}
			else {
				return array (
				'result' => false,
				'resCode' => '303',
				'resMessage' => 'error:Can not great floder';
			}
		}
	}

	function checkSize ($inputName,$fileMaxSize) {
	/*
	 * 检查上传文件尺寸
	 * $inputName,$fileMaxSize为必要参数 否则返回Lose the necessary parameters fileMaxSize单位为KB
	 * 如果尺寸超出指定范围 则返回 bigger than fileMaxSize
	 * 如果尺寸小于指定范围 则返回 smaller than fileMixSize
	 */
		if (empty($inputName) or empty($fileMaxSize)) {
			return array (
				'result' => false,
				'resCode' => '304',
				'resMessage' => 'error:Lose the necessary parameters';)
		}
		$imgSize = $_FILES[$inputName]["size"] ;
		$kSize = round($imgSize/1024) ;
		if($kSize > $fileMaxSize) {
			return array (
				'result' => false,
				'resCode' => '305',
				'resMessage' => 'error:bigger than fileMaxSize';)
		}
		return 'success';
	}

	function checkImgSize ($inputName,$MaxHeight,$MaxWidth,$MixHeight,$MixWidth) {
	/*
	 * 检查上传图片宽高是否符合范围
	 * 所有参数都为必要参数 否则返回 Lose the necessary parameters
	 * 图片尺寸超出最大宽高 返回 bigger than MaxHeight or MaxWidth
	 * 图片尺寸小于最小宽高 返回 smaller than MixHeight or MixWidth
	 * 图片尺寸在规定范围内 返回 true
	 *
	 */
	 	if (empty($inputName) or empty($MaxHeight) or empty($MaxWidth) or empty($MixHeight) or empty($MixWidth)) {return 'error:Lose the necessary parameters';}
		$imgSize = getimagesize($_FILES[$inputName]["tmp_name"]);
		if($imgSize[0] < $MixWidth or $imgSize[0] < $MixHeight) {
			return  array (
				'result' => false,
				'resCode' => '306',
				'resMessage' => 'error:smaller than MixHeight or MixWidth');
		}
		elseif($imgSize[0] > $MaxWidth or $imgSize[0] > $MaxHeight) {
			return  array (
				'result' => false,
				'resCode' => '307',
				'resMessage' => 'error:bigger than MaxHeight or MaxWidth');
		}
		else {return true;}
	}

	function getNewSize ($originalWidth,$originalHeight,$targetWidth,$targetHeight,$method) {
	/*
	 * 计算图片缩放比例
	 * $originalWidth,$originalHeight,$targetWidth,$targetHeight,$method 为必要参数 否则返回 error:getImgResizeProportion() Lose the necessary parameters
	 *
	 */
 		if (empty($originalWidth) or empty($originalHeight)or empty($targetWidth)or empty($targetHeight) or empty($method)) {return 'error:getImgResizeProportion() Lose the necessary parameters';}
		switch ($method) {
			case '01':
				//直接拉伸
				$newSize = array($originalWidth,$originalHeight);
				return $newSize;
				break;
		/*
			case '02':
				// 宽优先 水平置顶
				break;
			case '03':
				// 宽优先 水平居中
				break;
			case '04':
				// 宽优先 水平置底
				break;
			case '05':
				// 高优先 垂直居左
				break;
			case '06':
				// 高优先 垂直居左
				break;
			case '07':
				// 高优先 垂直居中
				break;
			case '08':
				// 高优先 垂直居右
				break;
			case '09':
				// 智能填充 左上对齐
				break;
			case '10':
				// 智能填充 右上对齐
				break;
			case '11':
				// 智能填充 左下对齐
				break;
			case '12':
				// 智能填充 右下对齐
				break;
			case '13':
				// 智能填充 居中对齐
				break;
		 */
			default:
				//不缩放
				$newSize = array($targetWidth,$targetHeight);
				return $newSize;
				break;
		}
	}


}
?>